package com.dabai.sas.activitys;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.dabai.sas.databinding.ActivityBaseEncodeBinding;
import com.dabai.utils.Base64;

import java.util.Objects;

public class BaseEncodeActivity extends AppCompatActivity {

    ActivityBaseEncodeBinding activityBaseEncodeBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityBaseEncodeBinding = ActivityBaseEncodeBinding.inflate(getLayoutInflater());
        setContentView(activityBaseEncodeBinding.getRoot());

        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

        activityBaseEncodeBinding.toBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String base = Base64.encode(activityBaseEncodeBinding.til1.getEditText().getText().toString());
                activityBaseEncodeBinding.til2.getEditText().setText(base);
            }
        });

        activityBaseEncodeBinding.toText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = Base64.decode(activityBaseEncodeBinding.til2.getEditText().getText().toString());
                activityBaseEncodeBinding.til1.getEditText().setText(text);
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}